package com.xiaohudie.servier.Impi;

import cn.hutool.core.util.RandomUtil;
import com.xiaohudie.pojo.AjaxResult;
import com.xiaohudie.servier.PhoneServicer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.concurrent.TimeUnit;

/**
 * @Author xiao hu die
 * @Date 2022/3/2 16:00
 * @Version 1.0
 */
@Slf4j
@Service
public class PhoneServierImpl implements PhoneServicer {
    @Resource
    private StringRedisTemplate stringRedisTemplate;
    @Override
    public String sendPhone(String phone, HttpSession session) {
     /**判断手机号是否为正确的*/
     if (phone.isEmpty()){
         return "手机号格式不对";
     }
     /*生成六位随机数*/
        String code = RandomUtil.randomNumbers(6);
        /**保存到session中*/
       // session.setAttribute("code",code);
       /**保存验证码到redis中
        * 设置验证码有效期为200秒*/
       stringRedisTemplate.opsForValue().set("login:code:"+phone,code,200000, TimeUnit.MILLISECONDS);
        /**发送验证码成功*/
       log.info("发送验证码成功");
        return code;
    }
}
